package com.group.view;

import javax.swing.*;
import java.awt.*;

import com.group.controller.StudentController;
import com.group.model.Student;

/**
 * @author lenovo
 */
public class AddStudentPanel extends JPanel {//继承面板类
    private JTextField nameField, genderField, birthField;
    private JButton addButton;
    private JLabel statusLabel;

    public AddStudentPanel() {
        setLayout(new GridLayout(5, 2, 10, 10));

        add(new JLabel("姓名:"));
        nameField = new JTextField();
        add(nameField);

        add(new JLabel("性别:"));
        genderField = new JTextField();
        add(genderField);

        add(new JLabel("出生日期 (yyyy-MM-dd):"));
        birthField = new JTextField();
        add(birthField);

        addButton = new JButton("添加学生");
        statusLabel = new JLabel("");

        add(addButton);
        add(statusLabel);
        //空的提示区域，点击按钮后用于显示结果

        addButton.addActionListener(e -> {
            String id = StudentController.generateId(); // 自动生成学号
            String name = nameField.getText().trim();
            String gender = genderField.getText().trim();
            String birth = birthField.getText().trim();

            Student student = new Student(id, name, gender, birth);
            StudentController.addStudent(student);
            statusLabel.setText("添加成功！学号：" + id);
        });
    }
}
